#pragma once

class BadMemoryAlloc
{};


template<class T>
class SimpleAllocator
{
public:
	SimpleAllocator(size_t InitSize = 10) : m_pBufferHead(0), m_BufferSize(0) {
		Resize(InitSize);
	}

	~SimpleAllocator() {
		::free(m_pBufferHead);
	}

	T *Resize(size_t NewEleNum) {
		T *pNewBuff = static_cast<T*>(::malloc(NewEleNum * sizeof(T)));
		if (pNewBuff == 0) throw BadMemoryAlloc();
		size_t CopyNum = NewEleNum < m_BufferSize ? NewEleNum : m_BufferSize;
		::memcpy(pNewBuff, m_pBufferHead, CopyNum * sizeof(T));
		::free(m_pBufferHead);
		m_pBufferHead = pNewBuff;
		m_BufferSize = NewEleNum;
		return m_pBufferHead;
	}

private:
	size_t m_BufferSize;
	T *m_pBufferHead;
};